package it.mozzicato.apkwizard.utils;

import java.io.*;
import java.util.*;

// Creato il 14/mar/2011

/**
 * @author Roberto Mozzicato
 *
 */
public class Configuration {
	public static final String PROJECT_DIR = "projectDir";
	public static final String LAST_IMPORT_DIR = "lastImportDir";
	
	private static Properties configuration;

	public static void loadConfig() {
		File file = getPropFile();
		if(file.exists())
			configuration = PropsUtils.load(file);
		else
			configuration = new Properties();
		
		System.out.println(configuration);		
	}

	private static File getPropFile() {
		String path = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		return new File(path + "/ApkWizard.props");
	}

	/**
	 * @param propertyName
	 * @return
	 */
	public static String getProperty(String propertyName) {
		return getProperty(propertyName, null);
	}

	public static String getProperty(String propertyName, String defaultValue) {
		if(configuration == null)
			loadConfig();
		return configuration.getProperty(propertyName, defaultValue);
	}

	public static String[] getPropertyAsStringArray(String propertyName) {
		String pos = Configuration.getProperty(propertyName);
		if(pos != null) {
			return pos.split(",");
		}
		return null;
	}

	public static int[] getPropertyAsIntArray(String propertyName) {
		String pos = Configuration.getProperty(propertyName);
		if(pos != null) {
			try {
				String[] split = pos.split(",");
				int[] ret = new int[split.length];
				for (int i=0; i < split.length; i++) {
					ret[i]=Integer.parseInt(split[i].trim());
				}
				return ret;
			}
			catch (Exception e) {}
		}
			
		return null;
	}

	/**
	 * @param propertyName
	 * @param value
	 */
	public static void setProperty(String propertyName, String value) {
		configuration.setProperty(propertyName, value);
		File file = getPropFile();
		PropsUtils.save(configuration, file);
	}
}
